package cn.edu.xmist.supermarket.cwz.controller;

import cn.edu.xmist.supermarket.cwz.config.Vars;
import cn.edu.xmist.supermarket.cwz.entity.chanpinPO;
import cn.edu.xmist.supermarket.cwz.entity.cangkuPO;
import cn.edu.xmist.supermarket.cwz.service.HouseService;
import cn.edu.xmist.supermarket.cwz.service.ProductService;
import cn.edu.xmist.supermarket.cwz.util.ObjtoLayJson;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 仓库控制器
 */
@RestController
@RequestMapping("/house")
public class CangkuKZQ {

  public static final String[] COLUMNS = {"id", "houseCode", "houseName"};

  @Autowired private HouseService houseService;
  @Autowired private ProductService wareService;
  /**
   * 仓库列表
   * @param page 页面
   * @param limit 分页
   * @return
   * @throws Exception
   */
  @RequestMapping("/list")
  public String list(Integer page, Integer limit) throws Exception {
    List<cangkuPO> list = houseService.list();

    return ObjtoLayJson.ListtoJson(list, COLUMNS);
  }
  /**
   * 仓库的更新，保存
   * @param house
   * @return
   */
  @RequestMapping("/saveOrUpdate")
  public String saveOrUpdate(cangkuPO house) {
    if (house.getId() == null) {
      QueryWrapper<cangkuPO> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("house_code", house.getHouseCode());
      List<cangkuPO> list = houseService.list(queryWrapper);
      if (list.size() > 0) {
        return Vars.FAILURE;
      }
    }
    Boolean flag = houseService.saveOrUpdate(house);
    return Vars.SUCCESS;
  }
  /**
   * 仓库的删除
   * @param id 地址
   * @return
   */
  @RequestMapping("/deleteById")
  public String deleteById(Long id) {
    cangkuPO house = houseService.getById(id);
    QueryWrapper<chanpinPO> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("house_code", house.getHouseCode());
    List<chanpinPO> list = wareService.list(queryWrapper);
    if (list.size() > 0) {
      return Vars.FAILURE;
    }
    Boolean flag = houseService.removeById(id);
    return Vars.SUCCESS;
  }
}
